/*
 * $Id:
 *
 * Copyright (C) 2011 Refiner Inc.
 * All right reserved
 *
 *============================================================
 * +rVPN Stack : AppIF (Socket Base Interface)
 *============================================================
 * abstract: This module provides TCP/IP stack interface
 * author  : MURATA
 * history :
 *
 */

#ifndef _RSI_TCPIPIF_H_
#define _RSI_TCPIPIF_H_

#include "rvs_appif.h"
#include "rvs_msg.h"
#include "rsi_apisock.h"

#define RSI_MAX_CONTS	256

/* TIFコンテキスト領域 */
typedef struct _rsi_tifct {
    uint16_t used;
    uint16_t msgcode;
    uint16_t seqno;
    uint16_t reserved;
    rsi_sock_t* rsock;
    void* args;
} rsi_tifct_t;

/* 関数別出力引数領域 */
typedef struct _accept_args {
    rvsaddr_t addr;
    socklen_t addrlen;
} accept_args_t;

//typedef accept_args_t recvfrom_args_t;
typedef struct _recvfrom_args {
    rvsaddr_t addr;
    socklen_t addrlen;
    char buffer[0];
} recvfrom_args_t;

typedef struct _recv_args {
    char buffer[1];
} recv_args_t;

typedef struct _getsockopt_args {
    int length;
    char buffer[0];
} getsockopt_args_t;

typedef accept_args_t getsockname_args_t;
typedef getsockopt_args_t ioctl_args_t;

typedef struct _select_args {
    int nfds;
    fd_set readfds;
    fd_set writefds;
    fd_set exceptfds;
    struct timeval tv;
} select_args_t;

typedef struct _status_args {
    rvs_status_t  status;
    rvs_error_t   error;
    const char *remotes[MAX_P2PCON];
} status_args_t;
 
#ifdef __cplusplus
extern "C"{
#endif

/* TIFコンテキスト領域の確保と解放 */
rsi_tifct_t* rsi_tif_new(int msgcode, rsi_sock_t* rsock);
void rsi_tif_free(rsi_tifct_t* ct);

/* 初期化 */
void rsi_tif_init(void);
void rsi_tif_stop(void);

/* リクエスト受付処理 */
int rsi_tif_request(rsi_sock_t* rsock);

/* エラー応答送信 */
void rsi_tif_senderr(rsi_tifct_t* ct, int err);

/* 応答送信関数群 */
void rsi_cb_socket(int result, void* context);
void rsi_cb_close(int result, void *context);
void rsi_cb_bind(int result, void *context);
void rsi_cb_listen(int result, void *context);
void rsi_cb_connect(int result, void *context);
void rsi_cb_accept(struct sockaddr *addr, socklen_t *addrlen, int result, void *context);
void rsi_cb_send(int result, void *context);
void rsi_cb_recv(void *buf, int result, void *context);
void rsi_cb_sendto(int result, void *context);
void rsi_cb_recvfrom(void *buf, struct sockaddr *from, socklen_t *fromlen, int result, void *context);
void rsi_cb_select(fd_set *in, fd_set *ou, fd_set *ex, int result, void *context);
void rsi_cb_ioctl(void* arg, int arglen, int result, void *context);
void rsi_cb_getpeername(struct sockaddr *name, socklen_t *namelen, int result, void *context);
void rsi_cb_getsockname(struct sockaddr *name, socklen_t *namelen, int result, void *context);
void rsi_cb_getsockopt(void *val, socklen_t *avalsize, int result, void *context);
void rsi_cb_setsockopt(int result, void *context);
void rsi_cb_shutdown(int result, void *context);
void rsi_cb_getlocalif(struct sockaddr* addr, socklen_t* addrlen, int result, void *context);
void rsi_cb_getvifroute(int vifno, int result, void *context);
void rsi_cb_statuscheck(rvs_status_t* statusp, rvs_error_t* errorp, int result, void *context);

#ifdef __cplusplus
}
#endif

#endif /* _RSI_TCPIPIF_H_ */
